import React, {PropsWithChildren, useEffect} from "react"
import {useNavigate} from "react-router";
import {observer} from "mobx-react-lite";
import employeeAuthorizationStore from "../store/EmployeeAuthorizationStore";

const WithAuthorization: React.FC<PropsWithChildren> = ({children}) => {
    let navigate = useNavigate();
    useEffect(() => {
        if (!employeeAuthorizationStore.hasLogIn) {
            navigate("/logIn", {
                replace: true,
            })
        }
    }, [employeeAuthorizationStore.hasLogIn]);

    return (
        <>
            {employeeAuthorizationStore.hasLogIn ? children : <></>}
        </>
    )
}

export default observer(WithAuthorization)